package com.lyf.boot.modules.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.lyf.boot.core.model.CascadeVo;
import com.lyf.boot.core.model.IdArrDto;
import com.lyf.boot.core.model.IdDto;
import com.lyf.boot.core.model.R;
import com.lyf.boot.modules.system.entity.dto.SysDeptDto;
import com.lyf.boot.modules.system.entity.vo.SysDeptVo;
import com.lyf.boot.modules.system.service.ISysDeptService;
import com.lyf.boot.validate.group.AddGroup;
import com.lyf.boot.validate.group.EditGroup;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 部门接口
 * @author Luyufan
 * @date 2023/7/7 9:24
 */
@RestController
@RequestMapping("/sysDept")
@RequiredArgsConstructor
public class SysDeptController {

    private final ISysDeptService sysDeptService;

    /**
     * 查询部门树
     * @return
     */
    @SaCheckPermission("sys:dept:list")
    @PostMapping("/getDeptList")
    public R<List<SysDeptVo>> getDeptList(){
        return R.ok(sysDeptService.getSysDeptList());
    }

    /**
     * 查询部门及联数据
     * @return
     */
    @PostMapping("/getDeptCascade")
    public R<List<CascadeVo>> getDeptCascade(){
        return R.ok(sysDeptService.getDeptCascade());
    }

    /**
     * 获取部门详情
     * @param dto
     * @return
     */
    @PostMapping("/getSysDeptInfo")
    public R<SysDeptVo> getSysDeptInfo(@RequestBody @Valid IdDto dto){
        return R.ok(sysDeptService.getSysDeptInfo(dto.getId()));
    }

    /**
     * 新增部门
     * @param dto
     * @return
     */
    @SaCheckPermission("sys:dept:add")
    @PostMapping("/saveSysDept")
    public R<Void> saveSysDept(@RequestBody @Validated(AddGroup.class) SysDeptDto dto){
        sysDeptService.saveSysDept(dto);
        return R.ok();
    }

    /**
     * 修改部门
     * @param dto
     * @return
     */
    @SaCheckPermission("sys:dept:update")
    @PostMapping("/updateSysDept")
    public R<Void> updateSysDept(@RequestBody @Validated(EditGroup.class) SysDeptDto dto){
        sysDeptService.updateSysDept(dto);
        return R.ok();
    }

    /**
     * 删除部门
     * @param dto
     * @return
     */
    @SaCheckPermission("sys:dept:delete")
    @PostMapping("/deleteSysDept")
    public R<Void> deleteSysDept(@RequestBody @Valid IdArrDto dto){
        sysDeptService.deleteSysDept(dto.getIds());
        return R.ok();
    }
}
